package com.appcenter.application.service;

import com.appcenter.application.input.Permission.*;
import com.appcenter.application.mapper.AppsMapper;
import com.appcenter.application.mapper.PermissionsMapper;
import com.appcenter.application.model.Dics;
import com.appcenter.application.model.Permissions;
import com.appcenter.application.output.Permission.*;
import com.appcenter.jz.config.ErrorJsonResult;
import com.appcenter.jz.page.PagedResult;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.UUID;

@Service
public class PermissionService {
    @Autowired
    PermissionsMapper permissionsMapper;

    @Autowired
    AppsMapper appsMapper;


    public CreatePermissionOutput CreatePermission(CreatePermissionInput input) {
        CreatePermissionOutput output = new CreatePermissionOutput();

        int NameCount = permissionsMapper.getNameCount(input.getItem().getAppId(), input.getItem().getPermissionName()).size();

        if (NameCount > 0) {
            throw new ErrorJsonResult("1000051", "权限名称已存在！");
        }
        String uuid = UUID.randomUUID().toString().replace("-", "").toLowerCase();
        Permissions Permission = new Permissions();
        Permission.setId(uuid);
        Permission.setAppId(input.getItem().getAppId());
        Permission.setPermissionName(input.getItem().getPermissionName());
        Permission.setLevel(1);
        Permission.setParentId(input.getItem().getParentId());
        Permission.setDescription(input.getItem().getDescription());
        if (input.getItem().getListOrder() != 0) {
            Permissions MaxListOrder = permissionsMapper.maxListOrder(input.getItem().getAppId());
            if (MaxListOrder == null) {
                Permission.setListOrder(10);
            } else {
                Permission.setListOrder(MaxListOrder.getListOrder() + 10);
            }
        }else{
            Permission.setListOrder(10);
        }
        permissionsMapper.insertSelective(Permission);
        output.setItem(Permission);
        return output;
    }

    public DeletePermissionOutput DeletePermission(DeletePermissionInput input) {
        DeletePermissionOutput output = new DeletePermissionOutput();
        permissionsMapper.deleteByPrimaryKey(input.getItem().getId());
        return output;
    }

    public EditPermissionOutput EditPermission(EditPermissionInput input) {
        EditPermissionOutput output = new EditPermissionOutput();
        int NameCount = permissionsMapper.getNameCountNoId(input.getItem().getAppId(), input.getItem().getPermissionName(), input.getItem().getId()).size();

        if (NameCount > 0) {
            throw new ErrorJsonResult("1000051", "权限名称已存在！");
        }
        Permissions Permission = permissionsMapper.selectByPrimaryKey(input.getItem().getId());
        Permission.setAppId(input.getItem().getAppId());
        Permission.setPermissionName(input.getItem().getPermissionName());
        Permission.setLevel(1);
        Permission.setParentId(input.getItem().getParentId());
        Permission.setDescription(input.getItem().getDescription());
        if (input.getItem().getListOrder() != 0) {
            Permissions MaxListOrder = permissionsMapper.maxListOrder(input.getItem().getAppId());
            if (MaxListOrder == null) {
                Permission.setListOrder(10);
            } else {
                Permission.setListOrder(MaxListOrder.getListOrder() + 10);
            }
        }else{
            Permission.setListOrder(10);
        }
        permissionsMapper.updateByPrimaryKey(Permission);
        output.setItem(Permission);
        return output;
    }

    public GetPermissionsOutput GetPermissions(GetPermissionsInput input) {
        GetPermissionsOutput output = new GetPermissionsOutput();
        PageHelper.startPage(input.getPage().getPageNo(),input.getPage().getPageSize(),"ListOrder desc");
        List<Permissions> ls = permissionsMapper.searchPermissionList(input);
        PageInfo<Permissions> page = new PageInfo<>(ls);
        output.setItems(new PagedResult<Permissions>(page.getList(),(int)page.getTotal()));
        return output;
    }

    public PageLoadOutput PageLoad(PageLoadInput input) {
        PageLoadOutput output = new PageLoadOutput();
        output.setItems(GetPermissions(input).getItems());
        output.setApps(appsMapper.selectAll());
        return output;
    }
}
